using System;
using System.ComponentModel;
using System.Windows.Forms;
using Toolbox.Plugin.File;
using System.ComponentModel.Composition;
using System.Collections.Generic;

namespace Toolbox.Plugin.Controls
{
    [ToolboxItem(typeof(IPluginSource))]
    public partial class SaveFilePluginMenuItem : PluginMenuItem
    {
        public SaveFilePluginMenuItem()
            : base()
        {
            InitializeComponent();
        }

        public override void OnImportsSatisfied()
        {
            this.BuildMenus<IPluginExporter>();
        }

        protected override void OnPluginItemClicked(object sender, EventArgs e)
        {
            if (sender is PluginToolStripItem)
            {
                this.SaveDialog.Tag = sender;
                this.SaveDialog.Title = "Saving " + (sender as PluginToolStripItem).PluginItem.Description;
                this.SaveDialog.Filter = "All files (*.*)|*.*";
                {
                    foreach (PluginFileAttribute attr in (sender as PluginToolStripItem).PluginItem.GetAttributes<PluginFileAttribute>())
                    {
                        this.SaveDialog.Filter = attr.FileType + " (*." + attr.Extension + ")|*." + attr.Extension + "|" + this.SaveDialog.Filter;
                    }
                }

                this.SaveDialog.ShowDialog();
            }

        }

        private void SaveDialog_FileOk(object sender, CancelEventArgs e)
        {
            if (!e.Cancel)
            {
                PluginToolStripItem tsi = this.SaveDialog.Tag as PluginToolStripItem;

                if (tsi is PluginToolStripItem && tsi.PluginItem.Plugin is IPluginImporter)
                {
                    (tsi.PluginItem.Plugin as IPluginImporter).FileName = this.SaveDialog.FileName;
                    base.OnPluginItemClicked(tsi, EventArgs.Empty);
                }
            }
        }

        //public event ProgressChangedEventHandler FileSaving;
        //public event RunWorkerCompletedEventHandler FileSaved;

        //protected override object OnDoWork(IPluginAction p)
        //{
        //    if (p is IPluginExporter)
        //    {
        //        object data = null;

        //        this.OnFileExport(ref data);

        //        (p as IPluginExporter).Save(data);

        //        return data;
        //    }
        //    else
        //    {
        //        return base.OnDoWork(p);
        //    }
        //}

        //protected virtual void OnFileExport(ref object data)
        //{
        //    this.ReportProgress(0);
        //}

        //protected override void OnProgressChanged(ProgressChangedEventArgs e)
        //{
        //    base.OnProgressChanged(e);

        //    if (this.FileSaving != null)
        //    {
        //        this.FileSaving(this, e);
        //    }
        //}

        //protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        //{
        //    base.OnRunWorkerCompleted(e);

        //    if (this.FileSaved != null)
        //    {
        //        this.FileSaved(this, e);
        //    }
        //}

       
    }

}
